package com.nit.lab.mapper.medical;

import com.nit.lab.dto.basic.UserDTO;
import com.nit.lab.dto.medical.InfoDTO;
import com.nit.lab.entity.medical.InfoEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Description
 * @Author HWY
 * @Date 2020/12/14 10:50
 **/
public interface InfoMapper {

    /**
     * @Description: 获取单位表格数据
     * @Author HWY
     * @Date 2020/12/14 10:50
     */
    @Select({"<script>SELECT " +
            "info_id AS `key`, info_id, " +
            "sensor_id, info_image " +
            "FROM medical_info " +
            "WHERE TRUE " +
            "<if test='sensorId != null'> AND sensor_id LIKE CONCAT('%', #{sensorId}, '%') </if>" +
            "ORDER BY " +
            "<if test='sortField != null'>" +
            "${sortField} ${sortOrder}, " +
            "</if>" +
            "info_id desc " +
            "LIMIT #{start}, #{length}" +
            "</script>"})
    List<InfoDTO> list4Table(
            @Param("start") Integer start,
            @Param("length") Integer length,
            @Param("sensorId") String sensorId,
            @Param("sortField") String sortField,
            @Param("sortOrder") String sortOrder) throws Exception;



    /**
     * @Description: 获取单位表格数据总条数
     * @Author HWY
     * @Date 2020/12/14 10:50
     */
    @Select({"<script>SELECT COUNT(*) " +
            "FROM medical_info " +
            "WHERE TRUE " +
            "<if test='sensorId != null'> AND sensor_id LIKE CONCAT('%', #{sensorId}, '%') </if>" +
            "</script>"})
    Long count4Table(
            @Param("sensorId") String sensorId) throws Exception;

    /**
     * @Description: 新增单位信息
     * @Author HWY
     * @Date 2020/12/14 10:50
     */
    @Insert("INSERT INTO medical_info (" +
            "sensor_id, " +
            "info_image, " +
            " gmt_create) " +
            "VALUES (#{infoEntity.sensorId}, " +
            "#{infoEntity.infoImage},  #{infoEntity.gmtCreate})")
    @Options(useGeneratedKeys = true, keyProperty = "infoId", keyColumn = "info_id")
    Integer add(@Param("infoEntity") InfoEntity infoEntity) throws Exception;

    /**
     * @Description: 更新单位信息
     * @Author HWY
     * @Date 2020/12/14 10:50
     */
    @Update("UPDATE medical_info " +
            "SET sensor_id = #{infoEntity.sensorId}, " +
            "info_image = #{infoEntity.infoImage} " +
            "WHERE info_id = #{infoEntity.infoId}")
    Integer update(@Param("infoEntity") InfoEntity infoEntity) throws Exception;

    /**
     * @Description: 删除单位信息
     * @Author HWY
     * @Date 2020/12/14 10:50
     */
    @Delete("DELETE FROM medical_info WHERE info_id = #{infoId}")
    Integer remove(@Param("infoId") Long infoId) throws Exception;

    @Select("SELECT dt_time, ecg_samples, id_session " +
            "FROM ecg_ecg_data ed, ecg_session es " +
            "WHERE TRUE and ed.id_session = es.id and " +
            "id_device = #{sensorId}"
            )
    List<InfoDTO> getInfo(
            @Param("sensorId") String sensorId) throws Exception;

    @Select("SELECT COUNT(*) count, id_session " +
            "FROM ecg_ecg_data ed, ecg_session es " +
            "WHERE TRUE and ed.id_session = es.id and " +
            "id_device = #{sensorId} " +
            "GROUP BY id_session"
            )
    List<InfoDTO> countInfo(
            @Param("sensorId") String sensorId) throws Exception;
}
